﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;

public partial class ucSearchAddUtilityGroup : System.Web.UI.UserControl
{
    UserManager _utilityGroupManager = new UserManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSearch.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSearch.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnAdd.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAdd.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnMoveUtility.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnMoveUtility.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnEdit.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnEdit.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnDelete.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnDelete.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        btnDelete.Attributes.Add("OnClick", "return confirmDelete()");
        if (!IsPostBack)
        {
            Session["UserManager_UtilityGroupSearch"] = "Latest";
            Search();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtSearch.Text == string.Empty)
            Session["UserManager_UtilityGroupSearch"] = "BlankSearch";
        else
            Session["UserManager_UtilityGroupSearch"] = txtSearch.Text;

        Search();   
    }
    protected void btnAdd_Click1(object sender, EventArgs e)
    {
        Session["UserManager_UtilityGroupEdit"] = "Add";
        Session["UserManager_Control"] = "UserManager/ucAddUtilityGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx"); 
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (CheckSelection())
        {
            Session["UserManager_UtilityGroupEdit"] = "Edit";
            Session["UserManager_Control"] = "UserManager/ucAddUtilityGroup.ascx";
            Response.Redirect("BusinessProcessAutomation.aspx");
        }

    }
    
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Delete();
    }
    protected void btnMoveUtility_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucUtilityMove.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx"); 
    }
    protected void lnbHome_Click(object sender, EventArgs e)
    {

    }
    protected void lnbUsers_Click1(object sender, EventArgs e)
    {

    }
    protected void lnbRoles_Click(object sender, EventArgs e)
    {

    }
    protected void lnbUtilities_Click(object sender, EventArgs e)
    {

    }
    protected void lnbPermissions_Click(object sender, EventArgs e)
    {

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucUtilityGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");     
    }
    void Search()
    {
        try
        {
            DataSet ds = new DataSet();
            _utilityGroupManager.SearchUtilityGroup(Session["UserManager_UtilityGroupSearch"].ToString(), out ds);

            gvwUtilityGroup.DataSource = ds;
            gvwUtilityGroup.Columns[1].Visible = true;
            gvwUtilityGroup.DataBind();
            gvwUtilityGroup.Columns[1].Visible = false;

            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('No Utilities Found !')</script>");
            }

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search UtilityGroup Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
    bool CheckSelection()
    {
        int count = 0, selectedRow = -1;

        for (int i = 0; i < gvwUtilityGroup.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwUtilityGroup.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                count++;
                selectedRow = i;
            }
        }

        if (count == 0)
        {
            Response.Write("<script>alert('Select Utility Group')</script>");
            return false;
        }
        else if (count > 1)
        {
            Response.Write("<script>alert('Select Single Utility Group')</script>");
            return false;
        }

        Session["UserManager_UtilityGroupID"] = gvwUtilityGroup.Rows[selectedRow].Cells[1].Text;
        return true;
    }
    void Delete()
    {
        Utility utilityGroup=new Utility();
        int index = 0;
       
        string[] utilityGroupIDs = new string[gvwUtilityGroup.Rows.Count];
        for (int i = 0; i < gvwUtilityGroup.Rows.Count; i++)
        {
            CheckBox chkGrid = (CheckBox)(gvwUtilityGroup.Rows[i].Cells[0].FindControl("chkEditDelete"));
            if (chkGrid.Checked == true)
            {
                utilityGroupIDs[index] = gvwUtilityGroup.Rows[i].Cells[1].Text;
                index++;
            }
        }

        if (index == 0)
        {
            Response.Write("<script>alert('Select Utility Group')</script>");
            return;
        }        

        string conditionUpdateUtilityList = "UT_UtilityGroupID='" + utilityGroupIDs[0] + "'";
        string conditionDeleteUtilityGroup = "UG_GroupID='" + utilityGroupIDs[0] + "'";
        for (int i = 1; i < index; i++)
        {
            conditionUpdateUtilityList = conditionUpdateUtilityList + "or UT_UtilityGroupID='" + utilityGroupIDs[i] + "'";
            conditionDeleteUtilityGroup = conditionDeleteUtilityGroup + "or UG_GroupID='" + utilityGroupIDs[i] + "'";    
        }
        _utilityGroupManager.DeleteUtilityGroup(conditionUpdateUtilityList, conditionDeleteUtilityGroup);
        Response.Write("<script>alert('Deleted Successfully !')</script>");
        Search();
    }

    protected void gvwUtilityGroup_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwUtilityGroup.PageIndex = e.NewPageIndex;
        Search();
    }
    protected void gvwUtilityGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
}
